(*  :Title:	Discrete-Time Fourier Transforms *)

(*  :Authors:	Brian Evans and James McClellan  *)

(*  :Summary:	To provide DTFT's not normally used by undergrads  *)

(*  :Context:	SignalProcessing`Digital`DTFT`     *)

(*  :PackageVersion:  2.7	*)

(*
    :Copyright:	Copyright 1989-1991 by Brian L. Evans
		Georgia Tech Research Corporation

	Permission to use, copy, modify, and distribute this software
	and its documentation for any purpose and without fee is
	hereby granted, provided that the above copyright notice
	appear in all copies and that both that copyright notice and
	this permission notice appear in supporting documentation,
	and that the name of the Georgia Tech Research Corporation,
	Georgia Tech, or Georgia Institute of Technology not be used
	in advertising or publicity pertaining to distribution of the
	software without specific, written prior permission.  Georgia
	Tech makes no representations about the suitability of this
	software for any purpose.  It is provided "as is" without
	express or implied warranty.
 *)

(*  :History:	begun and ended		June 18, 1992  *)

(*  :Keywords:	discrete-time Fourier transform  *)

(*
    :Source:	{Discrete-Time Signal Processing} by A. V. Oppenheim and
		  Ronald W. Schafer.  Prentice Hall.  1989
 *)

(*  :Warning:	*)

(*  :Mathematica Version:  1.2 or 2.0  *)

(*  :Limitation:  *)

(*
    :Discussion:  This file implements the forward and inverse DTFT's
		  not normally requested by undergraduates.
 *)

(*  :Functions:   *)



(*  B E G I N     P A C K A G E  *)

BeginPackage[ "SignalProcessing`Digital`MDDTFT`" ]
EndPackage[]

BeginPackage[ "SignalProcessing`Digital`DTFT`",
	      "SignalProcessing`Digital`DSupport`",
	      "SignalProcessing`Digital`InvZTransform`",
	      "SignalProcessing`Digital`ZTransform`",
	      "SignalProcessing`Digital`ZSupport`",
	      "SignalProcessing`Support`TransSupport`",
	      "SignalProcessing`Support`ROC`",
	      "SignalProcessing`Support`SigProc`",
	      "SignalProcessing`Support`LatticeTheory`",
	      "SignalProcessing`Support`SupCode`" ]


If [ TrueQ[ $VersionNumber >= 2.0 ],
     Off[ General::spell ];
     Off[ General::spell1 ] ];


Begin[ "`Private`" ]


(*  B E G I N     P A C K A G E  *)

postdownsamplemD[x_, w_, m_] :=
	Block [	{cosets, factor, invmtrans, k, newx},
		newx = posttransform[x];
		cosets = 2 Pi DistinctCosetVectors[m];
		factor = Abs[Det[m]];
		invmtrans = Inverse[Transpose[m]];
		Sum[ newx /.
		     ReplaceWith[ w, invmtrans . ( w - cosets[[k]] ) ],
		     {k, 1, factor} ] / factor ]

upsampleSetUp[l_, nvars_, x_, n_, w_, z_, options_, flag_ ] :=
	Block [	{lnew, nnew, pos, trash, wnew},
		{lnew, nnew, trash} =
			ReorderResampling[l, nvars, nList];
		pos = Flatten[ Map[Position[nList, #1]&, nnew] ];
		wnew = wList[[pos]];
		upsamplemD[ myDTFT[x, n, w, z, options, flag],
			    Complement[nnew, {n}], wnew, Transpose[lnew] ] ]

downsampleSetUp[m_, nvars_, x_, n_, w_, z_, options_, flag_ ] :=
	Block [	{mnew, nnew, pos, trash, wnew},
		{mnew, nnew, trash} =
			ReorderResampling[m, nvars, nList];
		pos = Flatten[ Map[Position[nList, #1]&, nnew] ];
		wnew = wList[[pos]];
		downsamplemD[ myDTFT[x, n, w, z, options, flag],
			      Complement[nnew, {n}], wnew, m ] ]


(* Routines to find an upsampling matrix and the upsampled variables *)

linearlyCoupledVariables[ x_, wlist_ ] :=
	Block [ {dims, i, newx, varlist},
		varlist = {};
		dims = Length[wlist];
		newx = Collect[ExpandAll[x], wlist];
                For [ i = 1, i <= dims, i++,
		      wvar = wlist[[i]];
		      goodvar = True;
		      newx /.
			( a_ wvar :> Block [ {}, goodvar = False; a wvar ] /;
			  goodvar && ! MyFreeQ[a, wlist] );
		      If [ goodvar, AppendTo[varlist, wvar] ] ];
		varlist ]

upsampleSystem[ x_, upvarlist_ ] := UpsampledSystem[x, upvarlist]

upsampleSystemQ[ x_, upvarlist_ ] :=
	Block [	{upmatrixtrans, wvars},
		{upmatrixtrans, wvars} = upsampleSystem[x, upvarlist];
		TrueQ[ Length[wvars] >= 2 ] && 
		! SameQ[ ResamplingMatrix[upmatrixtrans], False ] ]

undoUpsampling[ x_, w_, n_, z_, options_, flag_ ] :=
	Block [	{newx, nvars, pos, trace, upmatrixtrans, wvars},
		trace = SameQ[ Replace[Dialogue, options], All ];
		wvars = linearlyCoupledVariables[x, wList];
		{upmatrixtrans, wvars} = upsampleSystem[x, wvars];
		If [ trace,
		     Print["( after noticing that the DTFT is a function of"]; 
		     Print["  ", upmatrixtrans, " . ", wvars, " = ",
			   upmatrixtrans . wvars ];
		     Print["  although the matrix is unique only to within"];
		     Print["  a permutation of the rows )" ] ];
		pos = Flatten[ Map[Position[wList, #1]&, wvars] ];
		nvars = nList[[pos]];
		newx = x /. ReplaceWith[wvars, Inverse[upmatrixtrans] . wvars];
		Upsample[ Transpose[upmatrixtrans], nvars ] [
		  myinvDTFT[ SPSimplify[newx], w, n, z, options, flag ] ] ]


(*  E N D     P A C K A G E  *)

End[]
EndPackage[]

If [ TrueQ[ $VersionNumber >= 2.0 ],
     On[ General::spell ];
     On[ General::spell1 ] ];


(*  E N D I N G     M E S S A G E  *)

Print["Special multidimensional DTFT rules have been loaded."]
Null
